﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
    public class SystemLogManager
    {
        public static string Insert(string Action)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    SystemLog entity = new SystemLog();
                    entity.EmployeeID = AccountManager.CurrentAccount.EmployeeID;
                    entity.Time = DateTime.Now;
                    entity.Action = Action;
                    ctx.SystemLogs.Add(entity);
                    if (ctx.SaveChanges() > 0)
                        return "Thêm thành công";
                    else
                        return "Thêm thất bại";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        public string Delete(SystemLog t)
        {
            SystemLog entity = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    entity = ctx.SystemLogs.Where(a => a.SystemLogID == t.SystemLogID).FirstOrDefault<SystemLog>();
                    if (entity != null)
                    {
                        ctx.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                        ctx.SaveChanges();
                        return "Xóa thành công";
                    }
                    else
                        return "Mã nhật ký không tồn tại";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        public DataTable SearchByTime(DateTime Time)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var SystemLogList = (from a in ctx.SystemLogs where a.Time.Day == Time.Day && a.Time.Month == Time.Month && a.Time.Year == Time.Year select a).ToList<SystemLog>();
                foreach (var row in SystemLogList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        public SystemLog GetSingle(int Id)
        {
            SystemLog entity = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                ctx.Configuration.LazyLoadingEnabled = false;
                entity = ctx.SystemLogs.Where(a => a.SystemLogID == Id).FirstOrDefault<SystemLog>();
                ctx.Entry(entity).Reference(s => s.Employee).Load();
            }
            return entity;
        }


        public DataTable SearchByEmployeeID(string id)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var SystemLogList = (from a in ctx.SystemLogs where a.EmployeeID == id select a).ToList<SystemLog>();
                foreach (var row in SystemLogList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        private DataTable CreateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("SystemLogID", typeof(string));
            dt.Columns.Add("EmployeeID", typeof(string));
            dt.Columns.Add("Time", typeof(DateTime));
            dt.Columns.Add("Action", typeof(string));
            return dt;
        }

        public DataTable GetAll()
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var SystemLogList = (from a in ctx.SystemLogs orderby a.SystemLogID descending select a).ToList<SystemLog>();
                foreach (var row in SystemLogList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        public object[] CreateRow(SystemLog entity)
        {
            return new object[] { entity.SystemLogID, entity.EmployeeID, entity.Time, entity.Action};
        }
    }
}
